/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package banksystem;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;

/**
 *
 * @author conra_000
 */
public class AddressNormalizer {
	String mOriginalAddress = null;
	String mNormalizedAddress = null;
	String mLat = null;
	String mLng = null;
	
	AddressNormalizer(String address) {
		mOriginalAddress = address;
	}
	
	public void setOriginal(String address) {
		mOriginalAddress = address;
	}
	
	public String getOriginal() {
		return mOriginalAddress;
	}
	
	public String getNormalized() {
		return mNormalizedAddress;
	}
	
	public String getLatitute() {
		return mLat;
	}
	
	public String getLongetude() {
		return mLng;
	}
	
	private String getAddressApiHttps(String address) {
		 String addressReformat = address.replaceAll(" ", "+");
		
		return "https://maps.googleapis.com/maps/api/geocode/json?address=" +
				addressReformat +
				"&sensor=false";
	}
			
	
	public void GeoLookup() {
		
		String https_url = getAddressApiHttps(mOriginalAddress);
		
		URL url;
		try {
			
			url = new URL(https_url);
			HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();

			if(connection!=null){
				try {

					//System.out.println("****** Content of the URL ********");			
					BufferedReader br = new BufferedReader(
											new InputStreamReader(connection.getInputStream()));

					String input;
						String geoJson = new String();

					while ((input = br.readLine()) != null){
						geoJson += input + "\r\n";
					}
					br.close();

					mNormalizedAddress = findAddress(geoJson);
					findLatLng(geoJson);
					
				} catch (IOException e) {
					e.printStackTrace();
				} 
			}	 
		
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private String findAddress(String geoJson) {
		int beginIndex = geoJson.indexOf("formatted_address");
		if(beginIndex != -1) {
			beginIndex = geoJson.indexOf(" \"", beginIndex) + 2;
			int endIndex = geoJson.indexOf("\r", beginIndex);
			endIndex = geoJson.lastIndexOf("\"", endIndex);
			return geoJson.subSequence(beginIndex, endIndex).toString();
		}
		return null;
	}
	
	private void findLatLng(String geoJson) {
		int beginIndex = geoJson.indexOf("location");
		if(beginIndex != -1) {
			beginIndex = geoJson.indexOf("lat", beginIndex);
			beginIndex = geoJson.indexOf(":", beginIndex) + 2;
			int endIndex = geoJson.indexOf("\r", beginIndex) - 1;
			mLat = geoJson.subSequence(beginIndex, endIndex).toString();
			
			beginIndex = geoJson.indexOf("lng", beginIndex);
			beginIndex = geoJson.indexOf(":", beginIndex) + 2;
			endIndex = geoJson.indexOf("\r", beginIndex);
			mLng = geoJson.subSequence(beginIndex, endIndex).toString();
		}
	}
	
}
